package com.ambercrm.business;

import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ambercrm.R;
import com.ambercrm.base.BaseTitleActivity;
import com.ambercrm.constant.UrlConstant;
import com.ambercrm.custom_view.ClearEditText;
import com.ambercrm.custom_view.TextInputLayout;
import com.ambercrm.eventbus_event.LogoutEvent;
import com.ambercrm.tools.EmptyUtils;

import org.json.JSONObject;

import java.util.HashMap;

import de.greenrobot.event.EventBus;

public class ChangePasswordActivity extends BaseTitleActivity implements View.OnClickListener {

    private TextInputLayout phoneNumberTextInputLayout;
    private ClearEditText etPhone;
    private TextInputLayout codeTextInputLayout;
    private ClearEditText etCode;
    private TextView tvPswdOne;
    private TextView tvPswdTwo;
    private Button btnConfirm;
    private String phoneNumber;
    private String smsCode;
    private boolean onePswdShow = true;
    private boolean twoPswdShow = true;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_change_password;
    }

    @Override
    protected void initContentView(View v) {
        hideLine();
        Bundle bundle = getBundle();
        if (bundle != null) {
            phoneNumber = bundle.getString("number");
            smsCode = bundle.getString("code");
        }
        phoneNumberTextInputLayout = findViewById(R.id.phone_number_text_input_layout);


        etPhone = findViewById(R.id.et_phone);
        codeTextInputLayout = findViewById(R.id.code_text_input_layout);
        etCode = findViewById(R.id.et_code);


        tvPswdOne = findViewById(R.id.tv_pswd_show);
        tvPswdTwo = findViewById(R.id.tv_confirm_pswd);

        btnConfirm = findViewById(R.id.btn_confirm);


        etPhone.setHideDeleteIcon();
        etCode.setHideDeleteIcon();

        etPhone.setTransformationMethod(PasswordTransformationMethod.getInstance());
        etCode.setTransformationMethod(PasswordTransformationMethod.getInstance());

        tvPswdOne.setOnClickListener(this);
        tvPswdTwo.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);

        etPhone.setOnTextChangeListener(new ClearEditText.OnTextChangeListener() {
            @Override
            public void onTextChange(String s) {
                if (EmptyUtils.notEmpty(s)) {
                    tvPswdOne.setVisibility(View.VISIBLE);
                } else {
                    tvPswdOne.setVisibility(View.GONE);
                }
                checkBtnEnable();
            }
        });

        etCode.setOnTextChangeListener(new ClearEditText.OnTextChangeListener() {
            @Override
            public void onTextChange(String s) {
                if (EmptyUtils.notEmpty(s)) {
                    tvPswdTwo.setVisibility(View.VISIBLE);
                } else {
                    tvPswdTwo.setVisibility(View.GONE);
                }

                checkBtnEnable();
            }
        });
    }

    private void checkBtnEnable() {
        String number = etPhone.getText().toString().trim();
        String code = etCode.getText().toString().trim();
        if (EmptyUtils.isEmpty(number)) {
            btnConfirm.setEnabled(false);
            return;
        }

        if (EmptyUtils.isEmpty(code)) {
            btnConfirm.setEnabled(false);
            return;
        }

        btnConfirm.setEnabled(true);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_pswd_show) {
            onePswdShow = !onePswdShow;
            setOnePswdChange(onePswdShow);
        } else if (id == R.id.tv_confirm_pswd) {
            twoPswdShow = !twoPswdShow;
            setTwoPswdChange(twoPswdShow);
        } else if (id == R.id.btn_confirm) {
            submit();
        }
    }

    private void submit() {
        String number = etPhone.getText().toString().trim();
        String code = etCode.getText().toString().trim();

        if (!number.equals(code)) {
            toast("两次输入的密码不一致");
            return;
        }

        HashMap<String, Object> params = new HashMap<>();
        HashMap<String, Object> map = new HashMap<>();
        params.put("newPwd", number);
        params.put("confirmPwd", code);
        params.put("smsCode", smsCode);
        params.put("phone", phoneNumber);
        map.put("userdto", params);
        httpPost(UrlConstant.EDIT_PWD_URL, 0, map, false, "");
    }

    @Override
    protected void onSucceed(String result, int what) {
        super.onSucceed(result, what);
        try {
            JSONObject object = new JSONObject(result);
            String code = object.optString("code");
            String msg = object.optString("msg");
            JSONObject data = object.optJSONObject("result");
            if ("0".equals(code)) {
                toast("修改密码成功");
                finishActivity();
                EventBus.getDefault().post(new LogoutEvent("logout"));
            } else {
                toast(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setTwoPswdChange(boolean isShow) {
        if (isShow) {
            tvPswdTwo.setText("显示");
            etCode.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏密码
        } else {
            tvPswdTwo.setText("隐藏");
            etCode.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示密码
        }

        etCode.postInvalidate();
        CharSequence text = etCode.getText();
        if (text instanceof Spannable) {
            Spannable newSpannable = (Spannable) text;
            Selection.setSelection(newSpannable, text.length());
        }
    }

    private void setOnePswdChange(boolean isShow) {
        if (isShow) {
            tvPswdOne.setText("显示");
            etPhone.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            tvPswdOne.setText("隐藏");
            etPhone.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }

        etPhone.postInvalidate();
        CharSequence text = etPhone.getText();
        if (text instanceof Spannable) {
            Spannable newSpannable = (Spannable) text;
            Selection.setSelection(newSpannable, text.length());
        }
    }


}
